package com.dao.impl;

import com.dao.ClazzDao;
import com.dao.ClazzForStudentDao;
import com.entity.Clazz;
import com.entity.ClazzExample;
import com.mapper.ClazzMapper;
import com.page.PageInfo;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository("clazzDao")
public class ClazzDaoImpl implements ClazzDao, ClazzForStudentDao {
    @Resource
    ClazzMapper clazzMapper;

    @Override
    public List<Clazz> getAll() throws Exception {
        return clazzMapper.getAll();
    }

    @Override
    public int addClazz(Clazz clazz) throws Exception {
        return clazzMapper.insertSelective(clazz);
    }


    public Clazz getClazzByCno(String cno)throws  Exception{
       return clazzMapper.selectByPrimaryKey(cno);
    }
    public List<Clazz> getClazzsByPage(PageInfo pageInfo) throws Exception{
        //将用户选择 当前页码和每页大小传入给map
        int currPage = pageInfo.getCurrPage();
        int pageSize = pageInfo.getPageSize();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("currPage", currPage);
        map.put("pageSize", pageSize);
        System.out.println(pageInfo.getCurrPage() + " "+ pageInfo.getPageSize());
        //调用示例查询得到当前的总记录数
        pageInfo.setItemCount((int) clazzMapper.countByExample(new ClazzExample()));
        return null;
        //return clazzMapper.getClazzsByPage(map);
    }

    public List<Clazz> searchClazzByName(String name,PageInfo pageInfo) throws Exception{
        ClazzExample clazzExample = new ClazzExample();
        int limit = pageInfo.getPageSize();
        int offset = (pageInfo.getCurrPage() - 1) * limit;
        clazzExample.setOffset(offset);//就是原来的firstResult 就是通过（currPage - 1） * pageSize
        clazzExample.setLimit(limit);
        //criteria条件查询接口，添加条件

        //clazzExample.createCriteria().andNameLike(name);
        pageInfo.setItemCount((int)clazzMapper.countByExample(clazzExample));
      return clazzMapper.selectByExample(clazzExample);
    }

    public Clazz selectClazzWithScsAndCourse(String sno)throws Exception{
        return null;
        //return clazzMapper.selectClazzWithScsAndCourse(sno);
    }

    public int updateByPrimaryKeySelective(Clazz clazz){
        return clazzMapper.updateByPrimaryKeySelective(clazz);
    }
}
